/**
 * @ author: 汪孟苹
 */
define(["jquery", "template", "scprit/department/view/template/editMission.tpl.js"], function ($, template, tpl) {
    var editMissionInfo = {};
    var draweditMissionInfo = function (data) {
        return template("tpl_editMission_infoGroup", data);
    }
    var draweditMissionInfoRuleContent = function (data) {
        return template("tpl_editMission_ruleContent", data);
    }
    editMissionInfo.getDataMission = function (domId) {	// domId:基本信息的外裹domId:editMissionInfo
        var data = {};
        data.templateName = $("#" + domId + " input[name='missionName']").val();
        data.priority = $("#" + domId + " input[name='missionWeight']").val();
        data.type = $("#" + domId + " select[name='missionPushRule']").val();
        data.bannelUrl = $("#" + domId + " input[name='missionLogoUrl']").attr("logourl");
        if (data.type == "02" || data.type == "03") {
            var ageFromYear = $("#" + domId + " input[name='ageFromYear']").val();
            var ageFromMonth = $("#" + domId + " input[name='ageFromMonth']").val();
            var ageFromDay = $("#" + domId + " input[name='ageFromDay']").val();
            var ageToYear = $("#" + domId + " input[name='ageToYear']").val();
            var ageToMonth = $("#" + domId + " input[name='ageToMonth']").val();
            var ageToDay = $("#" + domId + " input[name='ageToDay']").val();
            if (ageFromYear == "") ageFromYear = 0;
            if (ageFromMonth == "") ageFromMonth = 0;
            if (ageFromDay == "") ageFromDay = 0;
            if (ageToYear == "") ageToYear = 0;
            if (ageToMonth == "") ageToMonth = 0;
            if (ageToDay == "") ageToDay = 0;
            var ageFrom = parseInt(ageFromYear * 365) + Math.round(ageFromMonth * 30.4) + parseInt(ageFromDay);
            var ageTo = parseInt(ageToYear * 365) + Math.round(ageToMonth * 30.4) + parseInt(ageToDay);
            data.pushRule = ageFrom + "-" + ageTo;
        } else if (data.type == "05") {
            var timeFrom = $("#" + domId + " input[name='timeFrom']").val();
            var timeTo = $("#" + domId + " input[name='timeTo']").val();
            if (timeFrom == "") timeFrom = 0;
            if (timeTo == "") timeTo = 0;
            data.pushRule = timeFrom + "-" + timeTo;
        } else {
            data.pushRule = $("#" + domId + " input[name='missionRuleContent']").val();
        }
        return data;
    }
    var ruleContentData = function (dataInfo) {
        var data;
        var defaultDataInfo = {
            type: "04",
            pushRule: null, // null, xxx, xxx-xxx
            ageYearFrom: null,
            ageMonthFrom: null,
            ageDayFrom: null,
            ageYearTo: null,
            ageMonthTo: null,
            ageDayTo: null,
            timeFrom: null,
            timeTo: null,
        };
        data = $.extend(defaultDataInfo, dataInfo);
        if (data.pushRule != "" && data.pushRule != null) {
            if (data.type == "02" || data.type == "03") {
                if (data.pushRule.indexOf('-') > -1) {
                    var arr = data.pushRule.split("-");
                    var ageYearFrom = Math.floor(arr[0] / 365);
                    var ageMonthFrom = Math.floor(arr[0] % 365 / 30.4);
                    var ageDayFrom = Math.round(arr[0] % 30.4);
                    var ageYearTo = Math.floor(arr[1] / 365);
                    var ageMonthTo = Math.floor(arr[1] % 365 / 30.4);
                    var ageDayTo = Math.round(arr[1] % 30.4);
                    data.ageYearFrom = ageYearFrom == 0 ? "" : ageYearFrom;
                    data.ageMonthFrom = ageMonthFrom == 0 ? "" : ageMonthFrom;
                    data.ageDayFrom = ageDayFrom == 0 ? "" : ageDayFrom;
                    data.ageYearTo = ageYearTo == 0 ? "" : ageYearTo;
                    data.ageMonthTo = ageMonthTo == 0 ? "" : ageMonthTo;
                    data.ageDayTo = ageDayTo == 0 ? "" : ageDayTo;
                }
            } else if (data.type == "05") {
                if (data.pushRule.indexOf('-') > -1) {
                    var arr = data.pushRule.split("-");
                    var timeFrom = arr[0];
                    var timeTo = arr[1];
                    data.timeFrom = timeFrom == 0 ? "" : timeFrom;
                    data.timeTo = timeFrom == 0 ? "" : timeTo;
                }
            }
        }
        return data;
    }
    editMissionInfo.init = function (options) {
        var defaultInfo = {};
        var dataInfo = {
            title: null,
            priority: null,
            type: "04",
            pushRule: null,
            bannelUrl: null,
        };
        defaultInfo.dataInfo = dataInfo;
        var opt = $.extend(defaultInfo, options);
        $("#" + options.id).html(tpl.init());
        var html = draweditMissionInfo(opt);
        $("#" + options.id).html(html);
    }
    editMissionInfo.ruleContentInit = function (options) {
        var data = {
            dataInfo: ruleContentData(options.dataInfo)
        }
        $("#" + options.id).html(tpl.ruleContentInit());
        var html = draweditMissionInfoRuleContent(data);
        $("#" + options.id).html(html);
    }
    return editMissionInfo;
});
